<template>
  <div>
    <home-head />
    <div class="wrap">
      <van-pull-refresh v-model="isLoading"
                        @refresh="onRefresh">
        <!-- 轮播图 -->
        <van-swipe class="my-swipe"
                   :autoplay="3000"
                   indicator-color="white">
          <van-swipe-item v-for="banner in banners"
                          :key="banner.id">
            <img :src="banner.picUrl"
                 alt="">
          </van-swipe-item>
        </van-swipe>
        <!-- /轮播图 -->
        <!-- 标签 -->
        <div class="evaluate">
          <div class="list">
            <van-icon name="passed"></van-icon>
            <span>认证龙头企业</span>
          </div>
          <div class="list">
            <van-icon name="passed"></van-icon>
            <span>16年品质</span>
          </div>
          <div class="list">
            <van-icon name="passed"></van-icon>
            <span>3小时送达</span>
          </div>
          <div class="list">
            <van-icon name="passed"></van-icon>
            <span>最近428812条好</span>
          </div>
        </div>
        <!-- 标签 -->
        <!-- 宫格导航 -->
        <van-grid clickable>
          <van-grid-item v-for="cate in cates"
                         :key="cate.id"
                         :icon="cate.icon"
                         :text="cate.name"
                         @click="ItemList(cate.id,cate.name)" />
        </van-grid>
        <!-- /宫格导航 -->
        <!-- 列表详情 -->
        <div class="items">
          <h2>送恋人/爱情鲜花</h2>
          <van-list v-model="loading"
                    :finished="finished"
                    finished-text="亲，到底了哦"
                    @load="onLoad">
            <home-items :items="items"
                        @clickDetail="ItemsDetail"
                        @HomeAddCart="addCart" />
          </van-list>
        </div>
        <!-- /列表详情 -->
      </van-pull-refresh>
    </div>
    <home-tabbar />
  </div>
</template>

<script>
import HomeTabbar from 'components/HomeTabbar'
import HomeHead from './components/HomeHead.vue'
import HomeItems from './components/HomeItems.vue'
import { fetchBanner, fetchCates, fetchItems } from 'api'
import mixin from 'utils/types'
export default {
  name: 'Home',
  data () {
    return {
      banners: [],
      cates: [],
      items: [],
      page: 1,
      pageSize: 5,
      isLoading: false,
      loading: false,
      finished: false
    }
  },
  created () {
    this.fetchBanner()
    this.fetchCates()
  },
  methods: {
    // addCart ({ id }) {
    //   if (!getToken()) {
    //     this.$toast.fail({
    //       message: '请先登录',
    //       duration: 1000,
    //       onClose: () => {
    //         this.$router.push({
    //           path: '/login',
    //           query: {
    //             from: this.$route.path
    //           }
    //         })
    //       }
    //     })
    //     return false
    //   }
    //   // 登录了，就加入购物车
    //   addCart({
    //     goodsId: id,
    //     number: 1,
    //     token: getToken()
    //   }).then(res => {
    //     if (res.data.code === 0) {
    //       this.$toast.success({
    //         message: '加入成功'
    //       })
    //       this.$store.commit('cart/set_items', res.data.data.items)
    //     }
    //   })
    // },
    ItemList (id, name) {
      this.$router.push(`/itemlist/${id}/${name}`)
    },
    /* ItemsDetail ({ id }) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    }, */
    /* onLoad () {
      this.fetchItems()
      this.page++
    },
    onRefresh () {
      this.page = 1
      this.items = []
      this.finished = false
      this.onLoad()
    }, */
    fetchBanner () {
      fetchBanner().then(res => {
        if (res.data.code === 0) {
          this.banners = res.data.data
        }
      })
    },
    fetchCates () {
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data.splice(0, 8)
        }
      })
    },
    fetchItems () {
      fetchItems({
        categoryId: 72399,
        page: this.page,
        pageSize: this.pageSize
      }).then(res => {
        this.isLoading = false
        if (res.data.code === 0) {
          this.loading = false
          this.items = [
            ...this.items,
            ...res.data.data.result
          ]
        }
        if (res.data.code === 700) {
          this.finished = true
        }
      })
    }
  },
  mixins: [mixin],
  components: {
    HomeTabbar,
    HomeHead,
    HomeItems
  }
}
</script>

<style lang="scss" scoped>
.my-swipe {
  width: 94%;
  border-radius: 12px;
  margin: 0 auto;
  padding-top: 6px;
  img {
    width: 100%;
  }
}
.evaluate {
  width: 94%;
  border-radius: 10px;
  font-size: 12px;
  display: flex;
  justify-content: space-evenly;
  color: #666;
  align-items: center;
  height: 26px;
  margin: 0 auto;
  .list {
    display: flex;
    align-items: center;
  }
}
.van-grid {
  width: 94%;
  border-radius: 12px;
  overflow: hidden;
  margin: 0 auto;
  /deep/.van-icon__image {
    width: 40px;
    height: 40px;
  }
}
.items {
  width: 94%;
  margin: 0 auto;
  h2 {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-top: 20px;
  }
}
</style>
